package api

import (
	"log"

	"gitee.com/dingiyan/goMyAdmin/db"
	"github.com/gin-gonic/gin"
)

func GetUserInfo(c *gin.Context) {

	email, _ := c.Get("email")
	log.Printf("登录的用户获取用户信息：email: %v", email)
	if email == nil {
		c.JSON(200, gin.H{
			"code": 1,
			"msg":  "用户信息异常",
		})
		return
	}
	user := &db.User{}
	err := db.DB.Where("email = ?", email).First(user).Error
	if err != nil {

		c.JSON(200, gin.H{
			"code": 2,
			"msg":  "用户信息异常",
		})
		return
	}

	if user.IsStop {
		c.JSON(200, gin.H{
			"code": 3,
			"msg":  "用户信息异常",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 0,
		"msg":  "success",
		// 前端格式
		"data": gin.H{
			"userId":   user.Email,
			"username": user.Name,
			"realName": user.Name,
			"avatar":   "",
			"roles":    []string{},
		},
	})
}
